package com.igs.muziapp;

import java.util.ArrayList;
import java.util.List;

import com.igs.muziapp.camera.CameraScaner;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class MuziMain extends Activity {

	private final String TAGD = "MuziMain";
	private int NowTab = 0;
	private Animation Anim_in_LeftToRight;
	private Animation Anim_in_RightToLeft;
	private Animation Anim_out_LeftToRight;
	private Animation Anim_out_RightToLeft;

	private ViewFlipper TabViewFlipper;
	//
	private ViewPager mPlayerDataViewPager;
	private ArrayList<View> mPlayerDataViews;
	private TabPlayerDataPagerAdapter tabPlayerDataAdapter;

	private Playdata mPlaydata;
	private Songinfo mSonginfo;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.muzi_main);

		// 主介面
		// mListViews = new ArrayList<View>();
		LayoutInflater mLi = LayoutInflater.from(this);

		TabViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
		TabViewFlipper.addView(mLi.inflate(R.layout.muzi_main_tab_playerdata, null));
		TabViewFlipper.addView(mLi.inflate(R.layout.muzi_main_tab_market, null));
		TabViewFlipper.addView(mLi.inflate(R.layout.muzi_main_tab_band, null));
		TabViewFlipper.addView(mLi.inflate(R.layout.muzi_main_tab_qrlogin, null));
		TabViewFlipper.addView(mLi.inflate(R.layout.muzi_main_tab_news, null));

		mPlayerDataViewPager = (ViewPager) TabViewFlipper.getChildAt(0).findViewById(R.id.viewpager);
		tabPlayerDataAdapter = new TabPlayerDataPagerAdapter();
		mPlayerDataViewPager.setAdapter(tabPlayerDataAdapter);
		mPlayerDataViews = new ArrayList<View>();
		mPlayerDataViews.add(mLi.inflate(R.layout.muzi_playerdata_tab01, null));
		mPlayerDataViews.add(mLi.inflate(R.layout.muzi_playerdata_tab02, null));

		mPlaydata = new Playdata(this);
		mPlayerDataViews.add(mPlaydata.getView());
		mSonginfo = new Songinfo(this);
		mPlayerDataViews.add(mSonginfo.getView());

		// 動作
		Anim_in_LeftToRight = AnimationUtils.loadAnimation(this, R.anim.in_lefttoright);
		Anim_in_RightToLeft = AnimationUtils.loadAnimation(this, R.anim.in_righttoleft);
		Anim_out_LeftToRight = AnimationUtils.loadAnimation(this, R.anim.out_lefttoright);
		Anim_out_RightToLeft = AnimationUtils.loadAnimation(this, R.anim.out_righttoleft);
		TabViewFlipper.setInAnimation(Anim_in_LeftToRight);
		TabViewFlipper.setOutAnimation(Anim_out_LeftToRight);

	}

	// ----------------------------------------------------------
	private class TabPlayerDataPagerAdapter extends PagerAdapter {
		@Override
		public int getCount() {

			// TODO Auto-generated method stub
			// Log.d(TAGD, "getCount():" + mListViews.size());
			return mPlayerDataViews.size();
		}

		@Override
		public Object instantiateItem(View collection, int position) {
			// Log.d(TAGD, "instantiateItem()");
			((ViewPager) collection).addView(mPlayerDataViews.get(position), 0);
			return mPlayerDataViews.get(position);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			// Log.d(TAGD, "isViewFromObject()");
			return arg0 == (arg1);
		}

		@Override
		public void destroyItem(View collection, int position, Object view) {
			// Log.d(TAGD, "destroyItem()");
			((ViewPager) collection).removeView(mPlayerDataViews.get(position));
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.muzi_main, menu);
		return true;
	}

	public void btn_playerdata(View v) {
		Log.d(TAGD, "btn_playerdata");

		if (NowTab == 0)
			return;
		NowTab = 0;
		TabViewFlipper.setDisplayedChild(NowTab);
		// mViewPager.setCurrentItem(0);

	}

	public void btn_market(View v) {
		Log.d(TAGD, "btn_market");
		if (NowTab == 1)
			return;
		NowTab = 1;
		TabViewFlipper.setDisplayedChild(NowTab);
		// mViewPager.setCurrentItem(1);
		// mViewPager.setCurrentItem(1);

	}

	public void btn_band(View v) {
		Log.d(TAGD, "btn_band");
		if (NowTab == 2)
			return;
		NowTab = 2;
		TabViewFlipper.setDisplayedChild(NowTab);
		// mViewPager.setCurrentItem(2);
	}

	public void btn_qrlogin(View v) {
		Log.d(TAGD, "btn_qrlogin");
		startActivity(new Intent(MuziMain.this, CameraScaner.class));
		
		// if (NowTab == 3)
		// return;
		// NowTab = 3;
		// TabViewFlipper.setDisplayedChild(NowTab);
		// mViewPager.setCurrentItem(3);
	}

	public void btn_news(View v) {
		Log.d(TAGD, "btn_news");
		if (NowTab == 4)
			return;
		NowTab = 4;
		TabViewFlipper.setDisplayedChild(NowTab);
		// mViewPager.setCurrentItem(4);
	}

}
